package com.king.web.browser.system.user;

import com.king.mapper.UserMapper;
import com.king.pojo.dto.UserDTO;
import com.king.pojo.entity.SysUser;
import com.king.pojo.query.UserQuery;
import com.king.pojo.vo.AjaxResult;
import com.king.pojo.vo.UserQueryVO;
import com.king.service.UserService;
import com.king.web.BasicController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户控制器
 *
 * @author liukai
 */
@RestController
@RequestMapping("/user")
public class UserController extends BasicController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/{id}")
    public ResponseEntity<SysUser> getOne(@PathVariable String id) {
        SysUser user = new SysUser();
        user.setUserId(id);
        user.setUsername("liukai");
        user.setPassword("123456");
        return new ResponseEntity<>(user, HttpStatus.OK);
    }

    @GetMapping("/query")
    public AjaxResult page(UserQuery userQuery, @PageableDefault Pageable pageable) {
        Map<String, Object> map = new HashMap<>(3);
        Page<SysUser> userList = userService.listUser(userQuery, pageable);
        List<UserQueryVO> content = userMapper.sysUserToUserVO(userList.getContent());
        map.put("Content", content);
        map.put("totalElements", userList.getTotalElements());
        map.put("totalPages", userList.getTotalPages());
        return success(map);
    }

    @PostMapping("/create")
    public AjaxResult create(@Valid UserDTO userDTO) {
        SysUser sysUser = userMapper.userDtosToSysUsers(userDTO);
        userService.insert(sysUser);
        return success();
    }


}
